package cn.neusoft.mapper;

import cn.neusoft.pojo.Food;
import cn.neusoft.query.BaseQuery;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FoodMapper extends BaseMapper<Food>{

    @Options(useGeneratedKeys = true,keyColumn = "foodId",keyProperty = "foodId")
    @Insert({"<script>" ,
            "insert into t_food(foodName, foodExplain, foodImg, foodPrice, businessId, remarks)" ,
            "        values(" ,
            "            #{foodName}," ,
            "            #{foodExplain}," ,
            "            #{foodImg}," ,
            "            #{foodPrice}," ,
            "            #{businessId}," ,
            "            #{remarks}" ,
            "        )" ,
            "</script>"})
    int add(Food t);

    @Update("update t_food set\n" +
            "                foodName=#{foodName},\n" +
            "                foodExplain=#{foodExplain},\n" +
            "                foodImg=#{foodImg},\n" +
            "                foodPrice=#{foodPrice},\n" +
            "                businessId=#{businessId},\n" +
            "                remarks=#{remarks}\n" +
            "                where foodId=#{foodId}")
    int update(Food t);

    @Delete("delete from t_food where foodId=#{foodId}")
    int delete(Integer id);

    @Select("select foodId, foodName, foodExplain, foodImg, foodPrice, businessId, remarks\n" +
            "        from t_food where foodId=#{foodId}")
    Food getById(Integer id);


    //  通过id引用FoodMapper.xml中的resultMap标签
    @ResultMap("foodBusinessMap")
    @Select({"<script>" +
            "select foodId, foodName, foodExplain, foodImg, foodPrice, f.businessId, f.remarks ," ,
            "               b.businessName" ,
            "        from t_food f left join t_business b on f.businessId=b.businessId" ,
            "<where>" ,
            "            <if test=\"foodName!=null and foodName!=''\">" ,
            "                and foodName like concat('%',#{foodName},'%')" ,
            "            </if>" ,
            "            <if test=\"businessId!=null and businessId!=0\">" ,
            "                and f.businessId=#{businessId}" ,
            "            </if>" ,
            "        </where>" ,
            "order by foodId desc" ,
            "</script>"})
    List<Food> getAll(BaseQuery baseQuery);

    @Select("select foodId, foodName, foodExplain, foodImg, foodPrice, businessId, remarks\n" +
            "        from t_food where businessId=#{businessId}")
    List<Food> getByBusinessId(Integer businessId);

}
